/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hfy.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import mavenFy.fyDownLoader.DownLoader;
import mavenFy.fyDownLoader.TaskBean;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @version 1.0
 * @author JOHN
 * 当前版本的DownLoaderUtil工具类仅可以一次下载一批m3u8的任务。在1.1版本中会支持添加多批m3u8任务；在1.2版本中会支持添加同时下载多批m3u8任务；
 */
public class DownLoaderUtil {
    private static String m3u8 = null;

    public static String getM3u8() {
        return m3u8;
    }

    public static void setM3u8(String m3u8) {
        DownLoaderUtil.m3u8 = m3u8;
    }
    
    /**
     * 根据m3u8的链接获取到ts文件的链接
     * @param m3u8
     * @return 
     */
    public static List<String> listTsHref(String m3u8){
        List<String> tsList = new LinkedList<String>();
        try {
            String ResourceParentPath = StringUtil.getResourceParentPath(m3u8);
            String host = null;
            try {
                URL url = new URL(m3u8);
                host = url.getProtocol()+"://"+url.getHost();
            } catch (MalformedURLException ex) {
                Logger.getLogger(DownLoaderUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
            OkHttpClient client = new OkHttpClient.Builder().build();
            Request request = new Request.Builder().url(m3u8).build();
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                BufferedReader br = new BufferedReader(new InputStreamReader(response.body().byteStream()));
                String line=null;
                while((line = br.readLine())!=null){
                    if(line.startsWith("#")){
                        continue;
                    }else if(line.startsWith("//")){
//                        System.out.println(host+line);
                        tsList.add(host+line);
                    }else{
//                        System.out.println(ResourceParentPath+line);
                        tsList.add(ResourceParentPath+line);
                    }
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(DownLoaderUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return tsList;
    }
    
    /**
     * 下载m3u8文件里面的ts文件
     * @param m3u8
     * @param parentFile
     * @throws Exception 
     */
    public static void downM3u8(String m3u8,File parentFile) throws Exception{
        List<String> tsList = listTsHref(m3u8);
        if(tsList == null){
            throw new Exception();
        }
        TaskBean task = null;
        DownLoader downLoad = new DownLoader();
        for(String ts:tsList){
            task = new TaskBean(ts,new File(parentFile,StringUtil.getsuffixByUrl(ts)+StringUtil.getprefixByUrl(ts)));
            downLoad.addTask(task);
        }
    }
}
